// pages/personal/sale/home.js

const app = getApp()
Page({

    /**
     * 页面的初始数据
     */
    data: {

    },

    /**
     * 生命周期函数--监听页面加载
     */
    onShow: function (options) {
        wx.showLoading({
            title: '加载中...',
        })
        app.http.get("shop-sale/index").then(res => {
            this.setData(res.data)
            if (res.data.saleMemberInfo == false) {
                this.apply()
            }
            wx.hideLoading()
        }).catch(err => {
            app.wxToast.error(err.message)
        })
    },

    apply() {
        const type = this.data.saleSetting.type
        if (type == 1) {
            wx.showLoading({
                title: '加载中...',
            })
            app.http.get("shop-sale-member/become-sale-member").then(res => {
                this.onShow()
            })
        } else if (type == 2) {
            wx.showModal({
                title: '提示',
                content: '您还不是分销商',
                confirmText: "立即申请",
                success: res => {
                    if (res.confirm) {
                        wx.redirectTo({
                            url: '../team/join?name=' + this.data.shareInfo,
                        })
                    } else {
                        wx.navigateBack()
                    }
                }
            })
        } else {
            wx.showModal({
                title: '提示',
                content: '你需要购买指定商品才能成为分销商',
                confirmText: "立即购买",
                success: res => {
                    if (res.confirm) {
                        wx.redirectTo({
                            url: '/pages/goods/detail/detail?id=' + this.data.saleSetting.goods_id,
                        })
                    } else {
                        wx.navigateBack()
                    }
                }
            })
        }
    },

    copyWx() {
        wx.setClipboardData({
            data: this.data.saleSetting.wx_num,
        })
        wx.showToast({
            title: '微信号已复制',
        })
    }
})